<?php
include('../../includes/classes/core.php');
$start  	= $_REQUEST['start'];
$end    	= $_REQUEST['end'];
$count 		= $_REQUEST["count"];
$action 	= $_REQUEST['act'];
$departament= $_REQUEST['departament'];
$type       = $_REQUEST['type'];
$category   = $_REQUEST['category'];
$s_category = $_REQUEST['sub_category'];
$done 		= $_REQUEST['done']%2;
$name 		= $_REQUEST['name'];
$title 		= $_REQUEST['title'];
$text[0] 	= "დავალებები შესრულებული დროის მიხედვით";
$text[1] 	= "'$departament'- საშუალო დრო";


$start = $db->convertDateTime($start);
$end = $db->convertDateTime($end);
//------------------------------------------------query-------------------------------------------
switch ($done){
	case  1:
		$result = mysql_query("SELECT crystal_users.`name` AS type,
										ROUND(SUM(TIMESTAMPDIFF(MINUTE,task.planned_end_date,task.fact_end_date))/COUNT(*)/60,2) AS time1
								FROM 	task
								JOIN  crystal_users ON task.responsible_user_id=crystal_users.id
								WHERE crystal_users.`name`='$departament'
								AND  `task`.`date` >= '$start' 
								AND  `task`.`date` <= '$end' 
								AND  `task`.`status` = 3");
		$text[0]=$text[1];
	break;
	default:
		$result = mysql_query("SELECT 	crystal_users.`name` AS d_name,
										COUNT(*),
										CONCAT(ROUND(COUNT(*)/(SELECT COUNT(*) 
																FROM task 
																JOIN crystal_users ON task.responsible_user_id=crystal_users.id
																WHERE `task`.`date` >= '$start' 
																AND   `task`.`date` <= '$end'
																AND   `task`.`status` = 3)*100,2),'%')
								FROM 		task
								JOIN crystal_users ON task.responsible_user_id=crystal_users.id
								WHERE `task`.`date` >= '$start' 
								AND   `task`.`date` <= '$end'
								AND   `task`.`status` = 3
								GROUP BY 	d_name");

		break;
}
///----------------------------------------------act------------------------------------------
switch ($action) {
	case "get_list":
		$data = array("aaData"	=> array());
		while ( $aRow = mysql_fetch_array( $result ) )
		{	$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				$row[0] = '0';

				$row[$i+1] = $aRow[$i];
			}
			$data['aaData'][] =$row;
		}
		echo json_encode($data); return 0;
		break;
	case 'get_category' :
		$rows = array();
		while($r = mysql_fetch_array($result)) {
			$row[0] = $r[0];
			$row[1] = (float) $r[1];
			$rows['data'][]=$row;
		}
		$rows['text']=$text[0];
		echo json_encode($rows);
		break;
		case 'get_in_page':
			mysql_query("SET @i = 0;");
			$rResult = mysql_query("SELECT	`task`.id,
										`task`.id,
										if(ISNULL(task.incomming_call_id), task.`date`,incomming_call.`date`) AS datee,
										incomming_call.pin,
										incomming_call.pid,
										if(ISNULL(task.incomming_call_id), task.`phone`, incomming_call.`phone`) AS datee,
										if(ISNULL(task.incomming_call_id), source.`name`, sou.`name` ) AS source,
										if(ISNULL(task.incomming_call_id), category.`name`, cat.`name` ) AS category,
										`crystal_users`.`name` ,
    									`crystal_users1`.`name` ,
										`status`.`call_status`,
										TIME_FORMAT(SEC_TO_TIME(asterisk_incomming.duration), '%i:%s') AS call_time,
										CONCAT('<button class=\'download\' str=',asterisk_incomming.file_name,'>მოსმენა</button>')
								FROM 					`task`
								LEFT JOIN 	incomming_call ON task.incomming_call_id=incomming_call.id
								LEFT JOIN 	`category` 		ON task.subcategory_id=category.id
								LEFT JOIN 	`category` AS cat 		ON incomming_call.call_category_id=cat.id
								LEFT JOIN 	`object` 		ON task.object_id=object.id
								LEFT JOIN 	`object` AS obj 		ON incomming_call.object_id=obj.id
								LEFT JOIN   asterisk_incomming ON incomming_call.uniqueid=asterisk_incomming.uniqueid
								LEFT JOIN 	source	AS sou	ON incomming_call.source_id=sou.id
								LEFT JOIN 	source ON task.task_source_id=source.id
								LEFT JOIN 	crystal_users ON task.responsible_user_id=crystal_users.id	
                                LEFT JOIN 	crystal_users   AS `crystal_users1`	ON incomming_call.user_id=crystal_users1.id
								LEFT JOIN 	`status`  	ON	task.`status`=`status`.`id`
								WHERE 			task.actived=1
								AND `task`.`date` >= '$start' and  `task`.`date` <= '$end'
								AND crystal_users.`name`='$_REQUEST[rid]' 
								AND task.`status`=3
								GROUP BY task.id");
					$data = array(
							"aaData"	=> array()
					);
		
					while ( $aRow = mysql_fetch_array( $rResult ) )
					{
					$row = array();
					$row1 = array();
		
					for ( $i = 0 ; $i < $count ; $i++ )
					{
					$row[] = $aRow[$i];
					$a=$aRow;
		
					{
		
					}
					}
						$data['aaData'][] = $row;
					}
					echo json_encode($data); return 0;
					break;
	default :
		echo "Action Is Null!";
		break;

}



?>